/**
 * 
 */
package cn.crm.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import cn.crm.bean.CustService;
import cn.crm.bean.Customer;
import cn.crm.bean.DDL;
import cn.crm.bean.Product;
import cn.crm.bean.User;
import cn.crm.dao.CustServiceDao;
import cn.crm.service.UserService;

/**
 * @author YY
 *
 */

@Repository("custServiceDao")
@SuppressWarnings("unchecked")
public class CustServiceDaoImpl extends HibernateDaoSupport implements
		CustServiceDao {


	@Override
	public List<CustService> custServiceList() {
		return super.getHibernateTemplate().find("from CustService where csIsdlete = 0");
	}

	
	@Override
	public boolean saveCustService(CustService custService) {
		try {
			super.getHibernateTemplate().save(custService);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}


	@Override
	public boolean updateCustService(CustService custService) {
		try {
			super.getHibernateTemplate().update(custService);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	
	@Override
	public boolean deleteCustService(CustService custService) {
		try {
			super.getHibernateTemplate().update(custService);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	
	@Override
	public CustService get(Integer id) {
	
		return super.getHibernateTemplate().get(CustService.class, id);
	}


	@Override
	public List<CustService> getCustServiceByPage(int curPage, int pageSize) {
		Session session = super.getSession();
		Query query = session.createQuery("from CustService where csIsdlete = 0 order by csServicetime DESC");
		query.setFirstResult((curPage-1)*pageSize);
		query.setMaxResults(pageSize);
		return query.list();
	}


	@Override
	public int getCount() {
		String hql = "select count(cs) from CustService cs where csIsdlete = 0";
		Session session = super.getSession();
		Query query = session.createQuery(hql);
		return Integer.parseInt(query.uniqueResult().toString());
	}


	@Override
	public List<CustService> getCustServiceByKey(Object...params) {
		DetachedCriteria criteria=DetachedCriteria.forClass(CustService.class);
//		criteria.add(Restrictions.eq("DDLByTypeServicestyle", style));
//		criteria.add(Restrictions.eq("DDLByTypeServicetype", type));
//		criteria.add(Restrictions.eq("DDLByTypeEffect", effect));
// 		criteria.add(Restrictions.eq("cust.custCid", "%"+customer+"%"));
		criteria.add(Restrictions.eq("csIsdlete", false));
		if (params!=null) {
			for (Object res : params) {
				if (res!=null) {
					criteria.add((Criterion)res);
				}
				
			}
		}
		return super.getHibernateTemplate().findByCriteria(criteria);
		
	}


	@Override
	public int getGroup(int id) {
		
		String hql="select count(cs) from CustService cs where csIsdlete = 0 and cust.custCid = '"+id+"'";
		Session session = super.getSession();
		Query query = session.createQuery(hql);
		return Integer.parseInt(query.uniqueResult().toString());
	}


	






}
